% Script to create half resolution files and create control points to do
% geometric transformation.
clear all
fclose all
close all


%% load images and scale to 0.5 save as .tif in path2
path1='C:\Users\Tom�s\Documents\1. Regehr Lab\Data\HCR\Registration and analysis\0. raw single Z\'; %background images are here.
path2='C:\Users\Tom�s\Documents\1. Regehr Lab\Data\HCR\Registration and analysis\1.Processed not registered\'; %processed channels are here use find string or change file names to all same format.
referencepath=[path2 's4b1' '\' ];

% get the folder contents
d2 = dir(path1);
% remove all files (isdir property is 0)
dfolders = d2([d2(:).isdir]==1);
% remove '.' and '..'
dfolders = dfolders(~ismember({dfolders(:).name},{'.','..'}));


channels={'Nxph1','Slc6a5','Aldh1a3'};
for i=1:numel(dfolders)   % slices
    
    backgroundFullRes=imread([dfolders(i).folder '\' dfolders(i).name '\' 'background.tif']); %Imagej format out for tif is .tif
    backgroundHalfRes = imresize(backgroundFullRes, 0.5, 'bicubic'); % scale to half resolution, bicubic interpolation.
    
    clear backgroundFullRes
    
    Nxph1FullRes=imread([path2 dfolders(i).name '\'  dfolders(i).name 'Nxph1Matlabout.tiff']); %Matlab format out for tif is .tiff
    Nxph1HalfRes=imresize(Nxph1FullRes, 0.5, 'bicubic');
    
    clear Nxph1FullRes
    
    Slc6a5FullRes=imread([path2 dfolders(i).name '\'  dfolders(i).name 'Slc6a5Matlabout.tiff']); %Matlab format out for tif is .tiff
    Slc6a5HalfRes=imresize(Slc6a5FullRes, 0.5, 'bicubic');
    
    clear Slc6a5FullRes
    
    Aldh1a3FullRes=imread([path2 dfolders(i).name '\'  dfolders(i).name 'Aldh1a3Matlabout.tiff']); %Matlab format out for tif is .tiff
    Aldh1a3HalfRes=imresize(Aldh1a3FullRes, 0.5, 'bicubic');
    
    clear Aldh1a3FullRes
    
    % save all half resolution images as .tiff
    imwrite(uint16(Nxph1HalfRes),[path2 dfolders(i).name '\' dfolders(i).name 'Nxph1HalfRes.tiff'])
    imwrite(uint16(Slc6a5HalfRes),[path2 dfolders(i).name '\' dfolders(i).name 'Slc6a5HalfRes.tiff'])
    imwrite(uint16(Aldh1a3HalfRes),[path2 dfolders(i).name '\' dfolders(i).name 'Aldh1a3HalfRes.tiff'])
    imwrite(uint16(backgroundHalfRes),[path2 dfolders(i).name '\' dfolders(i).name 'backgroundHalfRes.tiff'])

    
end


%% Create control points 
%Create a pair of control point vectors for each slice only third batch
%needed, all others have already been created variable names
%(e.g. fixedPoints1b2_4b1)

%Load reference image (s4b1 Nxph1 raw scaled to half resolution)
%     fixed=imread([referencepath 'REFERENCEHalfRes.tif']); %Imagej format out for tif is .tif
% 
%     tempslice='s7b3'
%     
%     moving=imread([path2  tempslice '\' tempslice 'backgroundHalfRes.tiff']); %Imagej format out for tif is .tif
% 
%     cpselect(moving*7,fixed*10)
%     
    
    path1='C:\Users\Tom�s\Documents\1. Regehr Lab\Data\HCR\';
    
    save([path1  'controlpointsbatch3.mat'])
    
    
